package cn.edu.ctbu.demo2.service.impl;

import cn.edu.ctbu.demo2.domain.Classes;

import cn.edu.ctbu.demo2.repository.ClassesReporsitory;
import cn.edu.ctbu.demo2.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import org.springframework.data.domain.Pageable;
import java.util.List;

@Service
public class ClassesimplService implements ClassesService {
    @Autowired
    private ClassesReporsitory studentReporsitory;

    public List<Classes>findAll(){
        return studentReporsitory.findAll();
    }

    //     只支持分页的查询
//    @param pageable
//    @return
    public Page<Classes> findAll(Pageable pageable){
        return studentReporsitory.findAll(pageable);
    }

    @Override
    public Page<Classes> finfAll(Example<Classes> student, Pageable pageable) {
        return studentReporsitory.findAll(student,pageable);
    }

    //    按id进行查询
//    @porom id 主键 整数
//    @return
    public Classes findById(Integer id){


        return studentReporsitory.findById(id).orElse(null);
    }

    //    按name进行查询,like
//    @porom name 主键 整数
//    @return
    public List<Classes> findByName(String name){

        return studentReporsitory.findByNameLike(name);
    }


    public Classes insert(Classes student){
        studentReporsitory.save(student);
        return student;
    }

    public Classes update(Classes student){
        studentReporsitory.save(student);
        return student;
    }

    public void delete(Classes student){
        studentReporsitory.delete(student);
    }

    public void delete(Long id){
        Classes student=new Classes();
        student.setId(Math.toIntExact(id));
        studentReporsitory.delete(student);
    }

    public void delete(Integer id){
        studentReporsitory.deleteById(id);
    }

    public Classes getById(Long id){
        Classes student=studentReporsitory.findById(Math.toIntExact(id)).orElse(null);
        return student;
    }

    public List<Classes> getByName(String name){
        List<Classes> students=studentReporsitory.findByNameLike(name);
        return students;
    }

}
